<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/23
 * Time: 15:19
 */

namespace app\logic\index\point;


use app\logic\BaseLogic;
use app\model\consume\ConsumePointLogModel;
use app\model\novel\NovelSpreadModel;
use app\model\tool\UserToolArticleModel;
use app\model\tool\UserToolVideoModel;
use app\model\user\UserModel;
use app\model\user\UserPromotionPlanModel;
use app\model\user\UserRechargeOrderModel;

class PointLogic extends BaseLogic
{
    protected $model;

    protected static $instance;

    public function __construct()
    {
        parent::__construct();
        $this->model = \app\model\user\UserPointFlowModel::getInstance();
    }

    public static function getInstance()
    {
//        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return  new self();
    }

    public function getList($param)
    {
        $where = [
            'user_sn'=>request()->curUserId,
        ];
        $type = $param['type']??1;
        if($type==1){
            $where[] = ['type','in',[1,3]];
        }else{
            $where['type'] = $type;
        }

        $fields = ['*'];
        $order = ['created_at', 'desc'];

        $list =  $this->model->index($where, $fields, $order);

        $consumeIds = [];
        foreach($list['data'] as $val){
            if($val['source_table']==ConsumePointLogModel::getInstance()->getTable()){
                $consumeIds[] = $val['sourceid'];
            }
        }
        $consumeIds && $consumeIds = array_unique($consumeIds);
        $consumeData = ConsumePointLogModel::getInstance()->getWhere([['id','in',$consumeIds]])->get()->toArray();
        $consumeMap = array_column($consumeData,null,'id');
        $novelMap = NovelSpreadModel::getInstance()->getWhere([['novel_sn','in',array_filter(array_column($consumeMap,'novel_sn'))]])->pluck('title','novel_sn')->toArray();

        foreach($list['data'] as $val){
            if($val['type']==1 && $val['way']==1 && $val['is_refund']==0){
                $val['name'] = '会员每日赠送';
            }elseif($val['type']==3 && $val['way']==1 && $val['is_refund']==0){
                $val['name'] = '体验会员赠送';
            }elseif($val['type']==2 && $val['way']==1 && $val['is_refund']==0){
                if($val['source_table']==UserModel::getInstance()->getTable()){
                    $val['name'] = '注册赠送';
                }else{
                    $val['name'] = '购买点数';
                }
            }elseif($val['type']==2 && $val['way']==2 && $val['source_table']==UserRechargeOrderModel::getInstance()->getTable()){
                $val['name'] = '退款·购买点数返还';
            }elseif($val['way']==2 || $val['is_refund']==1){
                if(isset($consumeMap[$val['sourceid']])){
                    if($consumeMap[$val['sourceid']]['plan_type']==1){
                        $title = isset($novelMap[$consumeMap[$val['sourceid']]['novel_sn']]) ?
                            $novelMap[$consumeMap[$val['sourceid']]['novel_sn']] : '';

                        $val['name'] = $title.' · '.'一键推文';
                    }elseif($consumeMap[$val['sourceid']]['plan_type']==2){
                        $val['name'] = '漫画推文';
                    }elseif($consumeMap[$val['sourceid']]['plan_type']==3){
                        $val['name'] = '爆款二创';
                    }elseif($consumeMap[$val['sourceid']]['plan_type']==4){
                        if($consumeMap[$val['sourceid']]['source_table']==UserToolVideoModel::getInstance()->getTable()){
                            $val['name'] = '去水印或提取文字'.' · '.'工具';
                        }elseif($consumeMap[$val['sourceid']]['source_table']==UserToolArticleModel::getInstance()->getTable()){
                            $val['name'] = '文章改编或智能分段'.' · '.'工具';
                        }else{
                            $val['name'] = $consumeMap[$val['sourceid']]['source_table'].' · '.'工具';
                        }
                    }elseif($consumeMap[$val['sourceid']]['plan_type']==5){
                        if(isset($consumeMap[$val['sourceid']]) && isset($novelMap[$consumeMap[$val['sourceid']]['novel_sn']])){
                            $title = $novelMap[$consumeMap[$val['sourceid']]['novel_sn']];
                            $val['name'] = $title.' · '.'海外推文';
                        }else{
                            $val['name'] = '海外推文'.' · '.'工具';
                        }
                    }
                    $val['name'] = ($val['is_refund']==1?'失败退点 · ':'').$val['name'];
                }
            }
            unset($val['source_table'],$val['sourceid']);
        }

        //$list['data'] = $this->unsetCommonFields($list['data'],['source_table','sourceid']);
        return $list;
    }
}